//
//  MYStatusretweetedView.swift
//  Sina
//
//  Created by 王亮龙1 on 16/5/25.
//  Copyright © 2016年 lianglong. All rights reserved.
//

import UIKit
import SnapKit

class MYStatusretweetedView: UIView {
    
    var bottomCons: Constraint?
    
    var statusViewModel: MYStatusViewModel? {
        didSet {
            //设置转发微博文字
            contentLabel.text = statusViewModel?.status?.retweeted_status?.text
            //注销约束
            bottomCons?.uninstall()
            
            //如果存在图片视图
            if let pics = statusViewModel?.status?.retweeted_status?.pic_urls where pics.count > 0 {
                
                pictureView.hidden = false
                //MARK:这里重新设置了图片视图的大小,更新了视图(重走数据源)
                pictureView.pictures = pics
                
                //重新给转发微博视图设置底部约束
                self.snp_updateConstraints(closure: { (make) in
                   self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
            } else {
                pictureView.hidden = true
                pictureView.pictures = nil
                
                self.snp_updateConstraints(closure: { (make) in
                    self.bottomCons = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
                
            }
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor(white: 0.85, alpha: 1)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
        
        addSubview(contentLabel)
        addSubview(pictureView)
        //不用设置高度,宽度,因为label的实例化方法中,设置了sizeToFit,和最大宽度
        contentLabel.snp_makeConstraints { (make) in
            make.top.left.equalTo(self).offset(StatusCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentLabel.snp_left)
        }
        
        self.snp_makeConstraints { (make) in
           self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
        
    }
    
    
    
    private lazy var contentLabel: UILabel = UILabel(title: "转发微博,我为自己代言", font: 14, textColor: UIColor.darkGrayColor(), alignment: .Left)
    private lazy var pictureView: MYStatusPictureView = MYStatusPictureView()
    

}
